package com.ruoyi.client.controller;

import cn.dev33.satoken.stp.StpUtil;
import com.ruoyi.client.domain.ShoppingCart;
import com.ruoyi.client.pojo.form.CartAddForm;
import com.ruoyi.client.pojo.vo.CartVo;
import com.ruoyi.client.service.IShoppingCartService;
import com.ruoyi.common.annotation.Anonymous;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.utils.bean.BeanUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.Valid;
import java.util.List;

/**
 * @Author：HuaQP
 * @Date：2024/4/23
 */
@Anonymous  //接口允许匿名访问
@RestController
@RequestMapping("/cart")
public class ShoppingCartController extends BaseController {
    private static final Logger logger = LoggerFactory.getLogger(ShoppingCartController.class);
    @Resource
    private IShoppingCartService cartService;

    //获取当前用户购物车列表的所有记录
    @GetMapping("/list")
    public AjaxResult list(){
        // 当前登录用户,未登录会抛出例外NotLoginException
        Long userId = StpUtil.getLoginIdAsLong();

        List<CartVo> list = cartService.list(userId);
        return AjaxResult.success(list);
    }

    // 添加购物车
    @PostMapping("/add")
    public AjaxResult add(@RequestBody @Valid CartAddForm form) {
        // 当前登录用户,未登录会抛出例外NotLoginException
        Long userId = StpUtil.getLoginIdAsLong();

        // 购物车项
        ShoppingCart cart = new ShoppingCart();
        BeanUtils.copyProperties(form,cart);
        cart.setUserId(userId); //这步很重要
        cart.setIsChecked(true); //默认勾选

        if(cartService.add(cart)){
            return AjaxResult.success();
        }else{
            return AjaxResult.error();
        }
    }

    // 删除购物车除（多个，逗号分割）
    @GetMapping("/delete")
    public AjaxResult delete(@RequestParam String ids){
        // 用户登录验证,未登录会抛出例外NotLoginException
        StpUtil.checkLogin();

        if(cartService.delete(ids)){
            return AjaxResult.success();
        }else{
            return AjaxResult.error();
        }
    }

    // 删除我的购物车中的全部商品（清空）
    @GetMapping("/deleteAll")
    public AjaxResult deleteAll(){
        // 当前登录用户,未登录会抛出例外NotLoginException
        Long userId = StpUtil.getLoginIdAsLong();

        if(cartService.deleteAll(userId)){
            return AjaxResult.success();
        }else{
            return AjaxResult.error();
        }
    }

    //修改购物车项购买数量
    @GetMapping("/updateCount")
    public AjaxResult updateCount(Long id,Long count){
        // 用户登录验证,未登录会抛出例外NotLoginException
        StpUtil.checkLogin();

        ShoppingCart cart = new ShoppingCart();
        cart.setId(id);
        cart.setCount(count);
        if(cartService.update(cart)){
            return AjaxResult.success();
        }else{
            return AjaxResult.error();
        }
    }

    //购物车项修改是否勾选值
    @GetMapping("/updateChecked")
    public AjaxResult updateChecked(Long id, Boolean isChecked){
        // 用户登录验证,未登录会抛出例外NotLoginException
        StpUtil.checkLogin();

        ShoppingCart cart = new ShoppingCart();
        cart.setId(id);
        cart.setIsChecked(isChecked);
        if(cartService.update(cart)){
            return AjaxResult.success();
        }else{
            return AjaxResult.error();
        }
    }

    //全选或全不选购物车是否勾选值
    @GetMapping("/updateCheckedAll")
    public AjaxResult updateCheckedAll(Boolean isChecked){
        // 当前登录用户,未登录会抛出例外NotLoginException
        Long userId = StpUtil.getLoginIdAsLong();

        if(cartService.updateCheckedAll(userId,isChecked)){
            return AjaxResult.success();
        }else{
            return AjaxResult.error();
        }
    }

    //获取当前用户购物车列表所有勾选商品记录
    @GetMapping("/listChecked")
    public AjaxResult listChecked(){
        // 当前登录用户,未登录会抛出例外NotLoginException
        Long userId = StpUtil.getLoginIdAsLong();
        return AjaxResult.success(cartService.listChecked(userId));
    }

}
